package cn.wolfcode.service.impl;


import cn.wolfcode.domain.Employee;

import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;

import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Collections;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;


    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRelation(id);

    }

    @Override
    public void save(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        if (ids != null && ids.length > 0) {
            for (Long rid : ids) {
                employeeMapper.insertRelation(employee.getId(), rid);
            }

        }

    }

    @Override
    public Employee get(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> listAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public void update(Employee employee, Long[] ids) {

        employeeMapper.updateByPrimaryKey(employee);
        employeeMapper.deleteRelation(employee.getId());
        if (ids != null && ids.length > 0) {
            for (Long rid : ids) {
                employeeMapper.insertRelation(employee.getId(), rid);
            }

        }
    }

    @Override
    public PageResult<Employee> query(QueryObject qo) {
        int count = employeeMapper.selectForCount(qo);
        if (count == 0) {
            return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(),
                    Collections.emptyList(), count);
        }
        int a = count % qo.getPageSize() == 0 ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
        if (qo.getCurrentPage() > a) {
            qo.setCurrentPage(a);
        }
        List<Employee> employees = employeeMapper.selectForList(qo);
        return new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), employees, count);


    }

    @Override
    public Employee login(String username, String password) {


        if (!StringUtils.hasText(username)) {
            throw new RuntimeException("用户名不能为空");
        }
        if (password == null || password.equals("")) {
            throw new RuntimeException("密码不能为空");
        }
        Employee employee = employeeMapper.selectByUsernameAndPassword(username, password);
        if (employee == null) {
            throw new RuntimeException("用户名或密码错误");

        }





        return employee;
    }

    @Override
    public void batchDelete(List ids) {
        employeeMapper.batchDelete(ids);
    }

   /* @Override
    public Employee selectByEmpPsd(String oldPassword) {
        if (!StringUtils.hasText(oldPassword)) {
            throw new RuntimeException("密码不能为空");
        }

        Employee employee = employeeMapper.selectByEmpPsd(oldPassword);

        return employee;
    }*/

   /* @Override
    public void updateByEmpId(Employee employee) {
        employeeMapper.updateByEmpId(employee);
    }
*/
    @Override
    public void updatePwd(String oldPassword, String newPassword) {
        if (oldPassword == null ||oldPassword.equals("")) {
            throw new RuntimeException("原密码不能为空");
        }
        if (newPassword == null || newPassword.equals("")) {
            throw new RuntimeException("新密码不能为空");
        }
        Employee currentUser = UserContext.getCurrentUser();
        String password = currentUser.getPassword();
        if (!password.equals(oldPassword)) {
            throw new RuntimeException("请输入正确的密码");

        }
        UserContext.getCurrentUser().setPassword(oldPassword);
      UserContext.getCurrentUser().setPassword(newPassword);
        employeeMapper.updateByPrimaryKey(currentUser);



    }


}















